$(function () {
    //新增的初始化必填信息
    validateRule()

    var settings = {
        url: ctx + "projectNum/list",
        pageSize: 10,
        queryParams: function (params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                projectNum: $(".projectNum-table-form").find("input[name='projectNum']").val() ? $(".projectNum-table-form").find("input[name='projectNum']").val() : '',
            };
        },
        columns: [{
            checkbox: true
        }, {
            field: 'id',
            visible: false
        }, {
            field: 'projectNum',
            title: '项目代码'
        }, {
            field: 'marketPlace',
            title: '市场名称'
        }, {
            field: 'customerName',
            title: '客户名称'
        }, {
            field: 'describes',
            title: '描述'
        }, {
            field: 'creationTime',
            title: '创建时间'
        }]
    };
    //表单重载
    $MB.initTable('projectNumTable', settings);

    /**
     * 按钮的监听
     */
    $("#projectNum-add .btn-save").click(function () {
        //按钮名称
        var name = $(this).attr("name");
        //新增表单信息
        var validator = $("#projectNum-add-form").validate();
        var flag = validator.form();
        if (flag) {
            //新增按钮
            if (name === "save") {
                $.post(ctx + "projectNum/add", $("#projectNum-add-form").serialize(), function (r) {
                    if (r.code === 0) {
                        //关闭窗口
                        closeModal();
                        $MB.n_success(r.msg);
                        $MB.refreshTable("projectNumTable");
                    } else $MB.n_danger(r.msg);
                });

                //修改按钮
            } else if (name === "update") {
                $.post(ctx + "projectNum/update", $("#projectNum-add-form").serialize(), function (r) {
                    if (r.code === 0) {
                        closeModal();
                        $MB.n_success(r.msg);
                        $MB.refreshTable("projectNumTable");
                    } else $MB.n_danger(r.msg);
                });
            }
        }
    });

    /**
     * 按钮关闭的监听
     */
    $("#projectNum-add .btn-close").click(function () {
        //按钮名称
        var name = $(this).attr("name");
        //关闭
        if (name === "projectNum-close") {
            //关闭页面
            closeModal();
            //清空表单
            $("#projectNum-add-form")[0].reset();
        }
    });

});


//搜索
function search() {
    $MB.refreshTable('projectNumTable');
}

//重置
function refresh() {
    $(".projectNum-table-form")[0].reset();
    search();
}

/**
 * 新增初始化信息必填信息
 */
function validateRule() {
    var icon = "<i class='zmdi zmdi-close-circle zmdi-hc-fw'></i> ";
    validator = $("#projectNum-add-form").validate({
        rules: {
            projectNum: {
                required: true
            },
            marketPlace: {
                required: true
            },
            customerName: {
                required: true
            },
            describes: {
                required: true
            },
        },
        messages: {
            projectNum: {
                required: icon + "请输入项目代号",
            },
            marketPlace: {
                required: icon + "请输入市场名称",
            },
            customerName: {
                required: icon + "请输入客户名称",
            },
            describes: {
                required: icon + "请输入描述",
            },

        }
    });
}

/**
 * 关闭窗口
 */
function closeModal() {
    $("#projectNum-add-button").attr("name", "save");
    $("#role-add-modal-title").html('新增项目代码');
    validator.resetForm();
    $MB.closeAndRestModal("projectNum-add");
}


/**
 * 删除信息
 */
function deleteProjectNum() {
    var rows = $("#projectNumTable").bootstrapTable('getSelections');
    var ids_arr = "";
    if (!rows.length) {
        $MB.n_warning("请勾选需要删除的信息！");
        return;
    }
    for (var i = 0; i < rows.length; i++) {
        ids_arr += rows[i].id;
        if (i !== (rows.length - 1)) ids_arr += ",";
    }
    $MB.confirm({
        text: "确定删除选中信息？",
        confirmButtonText: "确定删除"
    }, function () {
        $.post(ctx + 'projectNum/delete', {"id": ids_arr}, function (r) {
            if (r.code === 0) {
                $MB.n_success(r.msg);
                refresh();
            } else {
                $MB.n_danger(r.msg);
            }
        });
    });
}

/**
 * 修改数据
 */
function updateProjectNum() {
    var selected = $("#projectNumTable").bootstrapTable('getSelections');
    var selected_length = selected.length;
    if (!selected_length) {
        $MB.n_warning('请勾选需要修改的代号信息！');
        return;
    }
    if (selected_length > 1) {
        $MB.n_warning('一次只能修改一个信息！');
        return;
    }
    var $form = $('#projectNum-add');
    $form.modal();
    var projectNum = selected[0];
    $("#projectNum-add-modal-title").html('修改项目代号');
    $form.find("input[name='projectNum']").val(projectNum.projectNum);
    $form.find("input[name='id']").val(projectNum.id);
    $form.find("input[name='marketPlace']").val(projectNum.marketPlace);
    $form.find("input[name='customerName']").val(projectNum.customerName);
    $form.find("input[name='describes']").val(projectNum.describes);
    $("#projectNum-add-button").attr("name", "update");

}